# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=paraview
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.13.3
pkgrel=2
pkgdesc="Parallel Data Analysis and Visualization Application based on VTK. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.paraview.org/'
msys2_repository_url="https://gitlab.kitware.com/paraview/paraview"
msys2_references=(
  'archlinux: paraview'
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-cgns"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-gl2ps"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-liblas"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-openturns"
         "${MINGW_PACKAGE_PREFIX}-pdal"
         #"${MINGW_PACKAGE_PREFIX}-pegtl" https://gitlab.kitware.com/vtk/vtk/-/issues/18151
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-qt6-tools"
         "${MINGW_PACKAGE_PREFIX}-vtk")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-cli11"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-numpy"
            "${MINGW_PACKAGE_PREFIX}-python-matplotlib")
source=("https://www.paraview.org/files/v${pkgver%.*}/ParaView-v${pkgver}.tar.xz"
        "001-replace-vtk-glew-with-vtk-glad.patch::https://gitlab.kitware.com/paraview/paraview/-/commit/6e762b79d09ce3188992695a3817d5e166fec47f.patch"
        "002-replace-vtk-opengl-with-vtk-glad.patch::https://gitlab.kitware.com/paraview/paraview/-/commit/53bcd8cd82bfcd43813fc493bde7726272a28f40.patch"
        "003-replace-glew-includes-with-glad.patch::https://gitlab.kitware.com/paraview/paraview/-/commit/a5fe98f96e286e576235abb53053798cfd53512c.patch"
        "004-forward-opengl-context-init.patch::https://gitlab.kitware.com/paraview/paraview/-/commit/455fd10d7b0445e8cb63184665bf2b5a3810be16.patch"
        "005-fix-build-with-cli11-2.5.0.patch::https://gitlab.kitware.com/paraview/paraview/-/commit/e6b14df7.patch")
sha256sums=('3bd31bb56e07aa2af2a379895745bbc430c565518a363d935f2efc35b076df09'
            'a24c6162eda682d4eeb750432915711884e85dba6a55bf6222862ae0aa862611'
            'f8f1a79549e626cf25ea0f6ee9a3386a642690d5423d798833d4092f2a38c061'
            'eeb21c4afd826860173f4aa13b240eb86dc312a5601c566cd0880bd8f65255f5'
            'f87de9565bc6ef6b149b62c911a4d723ca6f6ae783a60ff66a1039080190f841'
            'cb4c65fa3de357d5344629b113d2bfd679548c2b760a60787ef52856ee98a260')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/ParaView-v${pkgver}

  apply_patch_with_msg \
    001-replace-vtk-glew-with-vtk-glad.patch \
    002-replace-vtk-opengl-with-vtk-glad.patch \
    003-replace-glew-includes-with-glad.patch \
    004-forward-opengl-context-init.patch \
    005-fix-build-with-cli11-2.5.0.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")

  CXXFLAGS+=" -w -Wno-register" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DPARAVIEW_BUILD_SHARED_LIBS=ON \
      -DPARAVIEW_USE_PYTHON=ON \
      -DPARAVIEW_USE_QT=ON \
      -DPARAVIEW_USE_MPI=OFF \
      -DPARAVIEW_ENABLE_GDAL=ON \
      -DPARAVIEW_ENABLE_LAS=ON \
      -DPARAVIEW_ENABLE_OPENTURNS=ON \
      -DPARAVIEW_ENABLE_PDAL=ON \
      -DPARAVIEW_VERSIONED_INSTALL=OFF \
      -DPARAVIEW_CUSTOM_LIBRARY_SUFFIX="pv${pkgver%.*}" \
      -DPARAVIEW_BUILD_WITH_EXTERNAL=ON \
      -DPARAVIEW_PYTHON_SITE_PACKAGES_SUFFIX="lib/python${_pyver}/site-packages/pv/" \
      -DPARAVIEW_IGNORE_CMAKE_CXX11_CHECKS=ON \
      -DPARAVIEW_USE_EXTERNAL_VTK=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      ../ParaView-v${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  install -Dm644 "${srcdir}/ParaView-v${pkgver}/Copyright.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
